/**
 *    CTR - Centro Tecnologia Rocci
 *    Open Source Java Classic Game Toolkit
 *
 *    (C) 2010, CTR - Centro Tecnologia Rocci
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 */
package it.ctr.game.toolkit.level.editor;

import it.ctr.game.toolkit.BrickBox;
import it.ctr.game.toolkit.component.Shell;
import it.ctr.game.toolkit.stage.level.LevelStage;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

import com.mojang.mario.level.Level;

public class ComponentViewer extends JComponent implements MouseListener, MouseMotionListener {
	private static final long serialVersionUID = -1234000000000000011L;

	private int y = -1;

	private Image[] image = new Image[5];

	public int selectedComponent;
	private LevelEditor brickChangedListener;

	public ComponentViewer() {
		Dimension size = new Dimension(150, Level.BLOCK_SIZE * 8);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	public void addNotify() {
		super.addNotify();
		BrickBox.init(getGraphicsConfiguration());
		image[0] = BrickBox.smallMario[0][0];
		image[1] = BrickBox.enemies[0][0];
		image[2] = BrickBox.enemies[0][1];
		image[3] = BrickBox.enemies[0][2];
		image[4] = BrickBox.enemies[0][3];
	}

	public void paintComponent(Graphics g)
    {   
    	int h = 1;
    	int w = 1;
    	for (int i = 0; i < 5; i++) {
    		w = (150 - image[i].getWidth(null))/2; 
    		g.drawImage(image[i], w, h, image[i].getWidth(null), image[i].getHeight(null),null);
    		if (y >= h && y <= h + image[i].getHeight(null)) {
    		     g.setColor(Color.BLACK);
    		     g.drawRect(w, h, image[i].getWidth(null), image[i].getHeight(null));
    	    }
    		if (i == selectedComponent) {
    			g.setColor(Color.BLUE);
    			g.drawRect(w, h, image[i].getWidth(null), image[i].getHeight(null));
    		}
    		h += image[i].getHeight(null);
    		
    	}
   
    }

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
		y = -1;
		repaint();
	}

	public void mousePressed(MouseEvent e) {
		int y = 1;
		for (int i = 0; i < 5; i++) {
			if (e.getY() >= y && e.getY() <= y + image[i].getHeight(null)) {
				setSelectedBrick(i);
				break;
			}
			y += image[i].getHeight(null);
		}
		repaint();
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseDragged(MouseEvent e) {
	}

	public void mouseMoved(MouseEvent e) {
		y = e.getY();
		repaint();
	}

	public void setSelectedBrick(int block) {
		selectedComponent = block;
		repaint();
	}

	public void addTilePickChangedListener(LevelEditor editor) {

	}
}